/***************************************************
> Copyright (C) 2024 ==KINGYI== All rights reserved.
> File Name: visitor_menu.c
> Author:MOKI
> Mail:1913964829@qq.com
> Created Time: 2024年09月13日 星期五 18时28分00秒
***************************************************/
#include "global.h"

void visitorManage()
{
    int choice = -1, quit_flag = 0;
    while (1)
    {
        printf("***************************************\n");
        printf("游客菜单：\n");
        printf("1.完善个人信息\n");
        printf("2.购买门票\n");
        printf("3.查看植物介绍\n");
        printf("4.查看园内环境信息\n");
        printf("0.返回上一级\n");
        printf("输入选择：");
        setbuf(stdin, NULL);
        scanf("%d", &choice);
        switch (choice)
        {
        case 1:
            updataVisitor();
            break;
        case 2:
            purchaseTicket();
            printf("购买成功");
            break;
        case 3:
            displayPlantIntro();
            break;
        case 4:
            EnvironmentData envirdata;
            envirdata = randEnvirData();
            printf("当前园内环境为：\n");
            printf("温度:%d\t湿度:%d\t光照:%d\t空气质量:%d\n",
                   envirdata.temperature, envirdata.humidity, envirdata.light, envirdata.air_quality);
            break;
        case 0:
            quit_flag = 1;
            break;
        default:
            printf("输入错误重新输入\n");
            break;
        }
        if (quit_flag == 1)
        {
            // strcpy(global_name_visitor, "");
            char empty[50]={0};
            fileWriteGlobal(VISITOR, empty);
            break;
        }
    }
}
void updataVisitor()
{
    char global_name_visitor[50];
    FILE *fp = fopen(GLOBAL_VARIABLE_FILE_PATH, "r");
    GlobalVariable global = {0};
    fread(&global,sizeof(GlobalVariable),1,fp);
    fclose(fp);
    strcpy(global_name_visitor, global.global_name_visitor);
    DataList *head_V = createVisitorList();
    DataList *end = head_V;
    char contact[20], password[20];
    int choice = -1, quit_flag = 0;
    while (end != NULL)
    {
        if (strcmp(global_name_visitor, end->datatype.visitor.name) == 0)
        {
            while (1)
            {
                printf("***************************************\n");
                printf("请选择更新的信息：\n");
                printf("1.更新密码\n");
                printf("2.更新联系方式\n");
                printf("0.返回上一级\n");
                printf("输入选择：");
                setbuf(stdin, NULL);
                scanf("%d", &choice);
                switch (choice)
                {
                case 1:
                    while (1)
                    {
                        printf("输入密码：");
                        setbuf(stdin, NULL);
                        scanf("%s", password);
                        printf("确认输入密码：");
                        setbuf(stdin, NULL);
                        scanf("%s", contact);
                        if (strcmp(password, contact) == 0)
                        {
                            strcpy(end->datatype.visitor.password, password);
                            printf("修改成功");
                            break;
                        }
                        else
                        {
                            printf("密码不一致请重新输入\n");
                            continue;
                        }
                    }
                    break;
                case 2:
                    printf("输入联系方式：");
                    setbuf(stdin, NULL);
                    scanf("%s", contact);
                    strcpy(end->datatype.visitor.contact, contact);
                    printf("修改成功");
                    break;
                case 0:
                    quit_flag = 1;
                    break;
                default:
                    break;
                }
                if (quit_flag == 1)
                {
                    break;
                }
            }
            break;
        }
        end = end->pn;
    }
    fileWriteVisitor(head_V);
    freeList(head_V);
}
void purchaseTicket()
{
    char global_name_visitor[50];
    FILE *fp = fopen(GLOBAL_VARIABLE_FILE_PATH, "r");
    GlobalVariable global = {0};
    fread(&global,sizeof(GlobalVariable),1,fp);
    fclose(fp);
    strcpy(global_name_visitor, global.global_name_visitor);
    DataList *head_V, *end = NULL;
    head_V = createVisitorList();
    end = head_V;
    while (end != NULL)
    {
        if (strcmp(end->datatype.visitor.name, global_name_visitor) == 0)
        {
            end->datatype.visitor.consumption += TICKET_FEE;
            break;
        }
        end = end->pn;
    }
    fileWriteVisitor(head_V);
    freeList(head_V);
}
void displayPlantIntro()
{
    DataList *head_P = NULL, *end = NULL;
    head_P = createPlantList();
    end = head_P;
    int count = 0;
    while (end != NULL)
    {
        count++;
        printf("%d\t植物名称：%s\t植物种类：%s\t植物生长状态：%s\n",
               count, end->datatype.plant.name, end->datatype.plant.species, end->datatype.plant.growth_status);
        end = end->pn;
    }
    freeList(head_P);
}